$(function(){

ipdepo = new ipdepo_class();

var rcpt  = new rcpt_users('RcptTab');
var wfrom = new white_from('FromTab');
var wsubn = new white_subn('wSubnTab');
var wreln = new white_reln('wRelnTab');
var breln = new black_reln('bRelnTab');
var orel  = new own_rel('oRelTab');
var opts  = new main_options('MainOpts');
var sconf = new save_config('iReload');
var mxstat = new mxlook_logs( 'mxStatTab' );
var avstat = new avir_logs( 'avStatTab' );
var allstat = new all_stat( 'allStatTab' )

ipdepo.rcpt  = rcpt;
ipdepo.wfrom = wfrom;
ipdepo.wsubn = wsubn;
ipdepo.wreln = wreln;
ipdepo.breln = breln;
ipdepo.orel  = orel;
ipdepo.opts  = opts;
ipdepo.sconf = sconf;
ipdepo.mxstat = mxstat;
ipdepo.avstat = avstat;
ipdepo.allstat = allstat;

ipdepo.unload_warn = true;

var apptasks = [
{   title: "Пользователи домена", disabled: false, active: false, tooltip: 'menu.antispam.rcptact',
    helpurl: 'helps/rcpt.html',
    show: function() { clear_state(); rcpt.show(); apptasks[0].active = true; return false; }, 
    hide: function(){ apptasks[0].active = false; rcpt.hide(); } 
},
{   title: "'Белые' адреса", disabled: false, active: false, tooltip: 'menu.antispam.whitefrom',
    helpurl: 'helps/wfrom.html',
    show: function() { clear_state(); wfrom.show(); apptasks[1].active = true; return false; }, 
    hide: function(){ apptasks[1].active = false; wfrom.hide(); }  
},
{ title: "'Белые' подсети", disabled: false, active: false, tooltip: 'menu.antispam.whitesubnets',
    helpurl: 'helps/wsubnets.html',
    show: function() { clear_state(); wsubn.show(); apptasks[2].active = true; return false; }, 
    hide: function(){ apptasks[2].active = false; wsubn.hide(); }   
},
{ title: "'Белые' релеи", disabled: false, active: false, tooltip: 'menu.antispam.whiterelnames', 
    helpurl: 'helps/wrelnames.html',
    show: function() { clear_state(); wreln.show(); apptasks[3].active = true; return false; }, 
    hide: function(){ apptasks[3].active = false; wreln.hide(); }
},
{ title: "'Черные' релеи", disabled: false, active: false, tooltip: 'menu.antispam.blackrelnames',
    helpurl: 'helps/brelnames.html',
    show: function() { clear_state(); breln.show(); apptasks[4].active = true; return false; }, 
    hide: function(){ apptasks[4].active = false; breln.hide(); }    
},
{ title: "Собственные релеи", disabled: false, active: false, tooltip: 'menu.antispam.ownrelays',
    helpurl: 'helps/ownrelnames.html',
    show: function() { clear_state(); orel.show(); apptasks[5].active = true; return false; }, 
    hide: function(){ apptasks[5].active = false; orel.hide(); }
},
{ title: "Опции фильтра", disabled: false, active: false, tooltip: 'menu.antispam.options',
    helpurl: 'helps/options.html',
    show: function() { clear_state(); opts.show(); apptasks[6].active = true; return false; }, 
    hide: function(){ apptasks[6].active = false; opts.hide(); }    
},
{ title: "Применить изменения", disabled: false, active: false, tooltip: 'menu.antispam.apply',
    helpurl: 'helps/main.html',
    show: function() { $(this).css('text-decoration', 'none'); sconf.show(); apptasks[7].active = true; return false; }, 
    hide: function(){ sconf.hide(); apptasks[7].active = false; }
},
{ title: "Антиспам защита", disabled: false, active: false, tooltip: 'menu.stat.spamlogtab',
    helpurl: 'helps/stataspam.html',
    show: function() { clear_state(); mxstat.show(); apptasks[8].active = true; return false; }, 
    hide: function(){ apptasks[8].active = false; mxstat.hide(); }
},
{ title: "Антивирусная защита", disabled: false, active: false, tooltip: 'menu.stat.virlogtab',
    helpurl: 'helps/statavir.html',
    show: function() { clear_state(); avstat.show(); apptasks[9].active = true; return false; }, 
    hide: function(){ apptasks[9].active = false; avstat.hide(); }
},
{ title: "Сводная статистика", disabled: false, active: false, tooltip: 'menu.statistic',
    helpurl: 'helps/statall.html',
    show: function() { clear_state(); allstat.show(); apptasks[10].active = true; return false; }, 
    hide: function(){ apptasks[10].active = false; allstat.hide(); }
}
];

ipdepo.apptasks = apptasks;
ipdepo.defhelpurl = 'helps/main.html';

$("#accordion").html('<h3><p>АнтиСпам фильтр</p></h3><ul>'+
    '<li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li>'+
    '<li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li>'+
    '</ul><h3><p>События и статистика</p></h3><ul>'+
    '<li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li></ul');

var acc_li_a = $("#accordion li a");

$("#accordion").accordion(
    {
        navigation: true,
        collapsible: true,
        autoHeight: false,
        icons: false
    }
);

acc_li_a.each( function (i) 
{
//    $(this).text(apptasks[i].title);
    $(this).html('<span style="width:100%;">'+apptasks[i].title+'</span>');
    $(this).attr({ systooltip: 'tooltips.php?ttname='+apptasks[i].tooltip});
});


function clear_state() {
    acc_li_a.each( function(i) 
    {
        if( apptasks[i].active ) {
            apptasks[i].hide();
            $(this).css('text-decoration', 'none');
        }
    });
} 

$('#accordion [systooltip]').cluetip({ 
        attribute: 'systooltip',
        cluetipClass: 'jtip',
        dropShadow: true,
        waitImage: false,
        positionBy: 'mouse',
        showTitle: false,
        escapeTitle: true,
        ajaxProcess: function(resp) {
            if( resp.errcode == '999' ) {
                alert( resp.errmess );
                ipdepo.go('login.php');
                return '';
            }
            return resp.data;
        },
        ajaxSettings: {
            dataType: 'json'
        },
        onActivate: function(e) {
            return ipdepo.tooltip;
        }
    });

    
// Индикация AJAX процесса
$('#busy_icon').ajaxStart(function() { $(this).show(); } );
$('#busy_icon').ajaxStop(function() { $(this).hide(); } );


// Установка среды и разрешений
var items = apptasks[0].tooltip;
for(i=1; i<apptasks.length; i++) items = items + ',' + apptasks[i].tooltip;
$.post('envinit.php', { oper: 'getenv', items: items }, function( rsp, textStatus)
{
    if( rsp.errcode ) {
        ipdepo.alert('AlertDlg', 'Ошибка', rsp.errmess, 'ipdepo-icon-stop32', ipdepo.go, 'index.php');
        return;
    }
    if( !$.isPlainObject(rsp.data) ) return;
    
    ipdepo.domain = rsp.data.domain;
    ipdepo.cat_id = rsp.data.cat_id;
    
    $('div#DomainName').html('Домен: <b>'+ipdepo.domain+'</b>');
    for( i=0; i<apptasks.length; i++ ) apptasks[i].disabled = rsp.data.items[i];

    acc_li_a.each( function (i) {
        if( apptasks[i].disabled ) {
            $(this).css({'color':'#ABB2AB'});
            $(this).addClass('.ui-state-disabled');
            $(this).click( function(){return false;} );
        }
        else {
            $(this).click( function() {$(this).css('text-decoration', 'underline'); return false;} );
            $(this).click( apptasks[i].show );
        }
    });    
}, 'json');


// Изменение размеров окна и скрол
set_scrollebility();
$(window).resize( set_scrollebility );

function set_scrollebility() {
    var wh = $(window).height();
    var ww = $(window).width();
    var mdiv = $('#main_panel');
    var mh = mdiv.outerHeight();
    var mw = mdiv.outerWidth();
    
    if( wh > mh && ww > mw ) {
        mdiv.removeClass('main_panel_static');
        mdiv.addClass('main_panel');
        $('html').css({'overflow':'hidden'});
    }
    else {
        mdiv.removeClass('main_panel');
        mdiv.addClass('main_panel_static');
        $('html').css({'overflow':'auto'});
    }   
};

if( $.browser.opera ) {
    opera.setOverrideHistoryNavigationMode('compatible');
}

/*
window.onunload = function(){
    $.post('destroy.php', { oper: 'destroysess' }, function( rsp, textStatus)
    { return; }, 'json');
    
    return true;
};
*/

window.onbeforeunload = function(ev){
var warn = false;
var msg = 'Вы изменили, но не применили настройки.\n Продолжить ?';

    if( !ipdepo.unload_warn ) { ipdepo.unload_warn = true; return; }
    
    $.ajaxSetup({async: false });
    $.post('opts/check_state.php', { oper: "checkreloadstate" }, function( rsp, textStatus)
    {
        if( $.isPlainObject(rsp.data) && (rsp.data.state & 1)) {
            warn = true;
        }
        $.ajaxSetup({async: true });
    }, 'json');

    if( warn ) {
        if( ev ) ev.returnValue = msg;
        return msg;
    }
};

/*
if( window.innerHeight && window.innerWidth ) {
    window.innerWidth = 1258;
    window.innerHeight = 780;
}
*/
//    white_from.show();


// Глобальное меню
// LogOut
$('#logout').html('<div class="ipdepo-icon-png16 ipdepo-img-logout16"></div>Выйти')
    .css({'float':'right'})
    .addClass('ipdepo-menubar-img16 ui-corner-all')
	.hover(
		function(){ $(this).addClass('ipdepo-hover-border'); },
		function(){ $(this).removeClass('ipdepo-hover-border'); }
	)
	.click( function(){ location.href = 'index.php'; } );

// ToolTip Switch
$('#swtooltips').html('<div class="ipdepo-icon-png16 ipdepo-img-quest16"></div>Подсказки')
    .css({'float':'right'})
    .addClass('ipdepo-menubar-img16 ui-corner-all')
	.hover(
		function(){ if( !ipdepo.tooltip ) $(this).addClass('ipdepo-hover-border'); },
		function(){ if( !ipdepo.tooltip ) $(this).removeClass('ipdepo-hover-border'); }
	)
	.click( function(){ ipdepo.tooltip = !ipdepo.tooltip; } );

// HelpBook
$('#helpbook').html('<div class="ipdepo-icon-png16 ipdepo-img-book16"></div>Документация')
    .css({'float':'right'})
    .addClass('ipdepo-menubar-img16 ui-corner-all')
	.hover(
		function(){ $(this).addClass('ipdepo-hover-border'); },
		function(){ $(this).removeClass('ipdepo-hover-border'); }
	)
	.click( function(){ ipdepo.help( 'HelpDlg', 'Документация', ipdepo ); } );
    
}); // End of ready function
